package edu.hm.activebadge.rfid.representation;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import edu.hm.activebadge.rfid.acquisition.badge.BadgeParameter;
import edu.hm.activebadge.rfid.acquisition.badge.TCPReceive;

public class ContextObject implements IContextRepresentation, Serializable {
	

	private String badge;
	private String tag;
	private boolean add;

	private GregorianCalendar timestamp;
	
	public ContextObject(String b, String t, boolean a, GregorianCalendar time){
		badge = b;
		tag = t;
		add = a;
		timestamp = time;
	}
	
	public ContextObject(ContextObject original){
		badge = original.getBadge();
		tag = original.getTag();
		add = original.getAdd();
		timestamp = original.getTimestamp();
	}
	
	
	public void setBadge(String b) {
		badge=b;

	}

	public String getBadge() {
		return badge;
	}

	public void setTimestamp(GregorianCalendar t) {
		timestamp = t;

	}

	public GregorianCalendar getTimestamp() {
		return timestamp;
	}

	public void setTag(String t) {
		tag = t;
	}

	public String getTag() {
		return tag;
	}

	public String identifyYourself() {
		
		return("Die UID "+tag+" wurde am "+timestamp.get(Calendar.DAY_OF_MONTH)+"."+timestamp.get(Calendar.MONTH)+"."+timestamp.get(Calendar.YEAR)+" um "+timestamp.get(Calendar.HOUR_OF_DAY)+":"+timestamp.get(Calendar.MINUTE)+":"+timestamp.get(Calendar.SECOND)+" durch den Badge "+badge+" erkannt");
	}
	
	public boolean isSameTag(IContextRepresentation toCompare){
		
		if(toCompare!=null)
			return(badge.equals(toCompare.getBadge())&&tag.equals(toCompare.getTag()));
		else
			return false;
	}

	
	public boolean isAdd() {
		return add;
	}

	public void setAdd(boolean add) {
		this.add = add;
	}

	public void capture(String b, String t, boolean a, GregorianCalendar ts) {
		badge = b;
		tag = t;
		add = a;
		timestamp = ts;
		
	}

	public void process() {

		Socket s;
		try {
			s = new Socket(BadgeParameter.getServer(), Integer.parseInt(BadgeParameter.getServerPort()));
			OutputStream os = s.getOutputStream();  
			ObjectOutputStream oos = new ObjectOutputStream(os);   
			oos.writeObject(new ContextObject(this));   
			oos.close();  
			os.close();  
			s.close(); 
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  

		
	}


	public boolean getAdd() {
		return add;
	}

}
